package com.vincent.template.controller.manage;

import com.vincent.template.commons.base.VctVo;
import com.vincent.template.dto.sys.role.RoleDto;
import com.vincent.template.service.logic.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Vincent Wang
 * @date 2021/2/28 20:26
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/manage/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @ApiOperation("查询所有角色")
    @GetMapping
    public List<RoleDto> findRoles() {
        return roleService.findRoles();
    }

    @ApiOperation("根据id查询角色")
    @GetMapping("/{id}")
    public RoleDto findRoleById(@PathVariable("id") Long id) {
        return roleService.findRoleById(id);
    }

    @ApiOperation("添加角色")
    @PostMapping("/create")
    public VctVo<Long> addRole(@Validated @RequestBody RoleDto roleDto) {
        return VctVo.id(roleService.addRole(roleDto));
    }

    @ApiOperation("更新角色")
    @PutMapping("/{id}/update")
    public boolean updateRole(@PathVariable("id") Long id, @Validated @RequestBody RoleDto roleDto) {
        return roleService.updateRole(id, roleDto);
    }

    @ApiOperation("删除角色")
    @DeleteMapping("/{id}/delete")
    public boolean deleteRole(@PathVariable("id") Long id) {
        return roleService.deleteRole(id);
    }
}
